    /** 
    * ALUIDirSecurity -  http://code.google.com/p/alui-toolbox/
    * Copyright (c) 2008-2009
    * by Fabien Sanglier
    *
    * This file is part of ALUIDirSecurity.
    * 
    * ALUIDirSecurity is free software: you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation, either version 3 of the License, or
    * (at your option) any later version.
    * 
    * ALUIDirSecurity is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    * GNU General Public License for more details.
    * 
    * You should have received a copy of the GNU General Public License
    * along with ALUIDirSecurity.  If not, see <http://www.gnu.org/licenses/>.
    *
    */

package com.alui.utils;

public class KDSecurityAgent {

	public static String APPNAME_PARAM_PREFIX = "-appname:";
	public static String LOGINTOKEN_PARAM_PREFIX = "-token:";
	public static String USERNAME_PARAM_PREFIX = "-username:";
	public static String PASSWORD_PARAM_PREFIX = "-password:";
	public static String FOLDERID_PARAM_PREFIX = "-folderid:";
	public static String DEBUG_PARAM_PREFIX = "-isdebug:";
	public static String CRAWLERIDS_PARAM_PREFIX = "-crawlerids:";
	public static String WEBPROPERTY_PARAM_PREFIX = "-webpropid:";
	public static String CRAWLERIDS_SEPARATOR = ";";
	
	/**
	 * This Class Replace the security on the cards based on their parent folder
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		String appname = "";
		String logintoken = "";
		String username = "";
		String password = "";
		int topfolderid = -1;
		int webpropid = -1;
		String[] crawlerids = null;
		boolean isDebug = false;
		
		int exitlevel = 0;
		
		try {
			//iterate through the params and assign the appropriate one to the right variable
			if(null != args && args.length > 0){
				for(int i=0; i<args.length; i++){
					if(args[i].indexOf(APPNAME_PARAM_PREFIX) > -1){
						appname = args[i].substring(APPNAME_PARAM_PREFIX.length());
					} else if(args[i].indexOf(LOGINTOKEN_PARAM_PREFIX) > -1){
						logintoken = args[i].substring(LOGINTOKEN_PARAM_PREFIX.length());
					} else if(args[i].indexOf(USERNAME_PARAM_PREFIX) > -1){
						username = args[i].substring(USERNAME_PARAM_PREFIX.length());
					} else if(args[i].indexOf(PASSWORD_PARAM_PREFIX) > -1){
						password = args[i].substring(PASSWORD_PARAM_PREFIX.length());
					} else if(args[i].indexOf(FOLDERID_PARAM_PREFIX) > -1){
						topfolderid = Integer.parseInt(args[i].substring(FOLDERID_PARAM_PREFIX.length()));
					} else if(args[i].indexOf(DEBUG_PARAM_PREFIX) > -1){
						isDebug = Boolean.valueOf(args[i].substring(DEBUG_PARAM_PREFIX.length())).booleanValue();
					}  else if(args[i].indexOf(WEBPROPERTY_PARAM_PREFIX) > -1){
						webpropid = Integer.parseInt(args[i].substring(WEBPROPERTY_PARAM_PREFIX.length()));
					}  else if(args[i].indexOf(CRAWLERIDS_PARAM_PREFIX) > -1){
						crawlerids = args[i].substring(CRAWLERIDS_PARAM_PREFIX.length()).split(CRAWLERIDS_SEPARATOR);
					} else {
						//ignore the param
						System.out.println("The parameter [" + args[i] + "] is not recognized...hence ignored.");
					}
				}
			}
			
			if(isDebug){
				System.out.println("appname:"+ appname);
				System.out.println("logintoken:"+ appname);
				System.out.println("username:"+ username);
				System.out.println("password:"+ "***********");
				System.out.println("topfolderid:"+topfolderid);
				System.out.println("webpropid:"+webpropid);
				if(crawlerids != null){
					for(int i = 0; i< crawlerids.length; i++){
						System.out.println("crawlerid [" + i+1 + "]:" + crawlerids[i]);
					}
				}
			}
			
			// continue if the right params have been provided
			if (!"".equals(appname) && topfolderid != -1
					&& (!"".equals(logintoken) || (!"".equals(username)))) {

				FolderSecurityUtil fsUtil = null;

				if (!"".equals(logintoken))
					fsUtil = new FolderSecurityUtil(appname, logintoken,isDebug);
				else
					fsUtil = new FolderSecurityUtil(appname, username,password, isDebug);

				fsUtil.setShowUnapproved(true);
				fsUtil.addPTCrawlerids(crawlerids);
				
				fsUtil.propagateACLsFromFolder(topfolderid, webpropid);

				//output summary
				System.out.println("The operation completed in " + fsUtil.getTimingInMs() + " ms.");
				System.out.println("The operation processed " + fsUtil.getProcessedcards() + " cards with " + fsUtil.getCardprocesserrorcount() + " error(s).");
				
				// everything went well if no exception was fired.
				exitlevel = 0;
			} else {
				throw new IllegalArgumentException(
						"A mandatory argument is not provided.");
			}
		} catch (IllegalArgumentException iex) {
			System.err.println("An error occured with the entered parameters. Please enter all the required parameters."
					+ "Param Usage: \n" + 
					APPNAME_PARAM_PREFIX + "<appname>" + "\n" +
					LOGINTOKEN_PARAM_PREFIX + "<logintoken>" + "\n" +
					FOLDERID_PARAM_PREFIX + "<folderid>" + "\n" +
					DEBUG_PARAM_PREFIX + "<true|false>" + "\n" +
					USERNAME_PARAM_PREFIX + "<username>" + "\n" +
					PASSWORD_PARAM_PREFIX + "<password>");
			iex.printStackTrace(System.err);
			
			exitlevel = 3;
		}
		catch (Exception ex) {
			System.err.println("An unexpected error occured.");
			ex.printStackTrace(System.err);
			exitlevel = 7;
		} finally {
			System.exit(exitlevel);
		}
	}
}
